package com.example.securitydemo.handler;

import com.example.core.libs.JSONResult;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestControllerAdvice
@Order(value = Ordered.HIGHEST_PRECEDENCE)//提高优先级
public class GlobalExceptionHandler {

    @ExceptionHandler(value = AccessDeniedException.class)
    @ResponseBody
    protected JSONResult handleAppAccessDenied(AccessDeniedException e, HttpServletResponse response) throws IOException {
        return new JSONResult(999, "你没有权限进行此操作");
    }
}
